package com.joysuch.wwyt.workflow.repository;

import com.joysuch.wwyt.workflow.entity.WorkFlowProcess;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.persistence.Tuple;
import java.util.List;

public interface WorkFlowProcessDao extends JpaRepository<WorkFlowProcess, Long> {


    public Page<WorkFlowProcess> findAll(Pageable pageable);

    public WorkFlowProcess save(WorkFlowProcess entity);

    WorkFlowProcess findByModelId(String modelId);

    List<WorkFlowProcess> findAll();

    WorkFlowProcess findByBizName(String bizName);

    List<WorkFlowProcess> findByLocationServiceOpen(boolean locationServiceOpen);


    @Query(value = "select * from wwyt_workflow_process where delete_flag = '0'and location_service_open = 1 and biz_type_name in :names", nativeQuery = true)
    List<WorkFlowProcess> findByBizTypeNames(@Param("names") List<String> names);

    @Query(value = "select * from wwyt_workflow_process where delete_flag = '0'and location_service_open = 1 and biz_name in :names and org_code like concat(:orgCode,'%') and tenent_id = :tenentId", nativeQuery = true)
    List<WorkFlowProcess> findByBizNamesAndCompany(@Param("names") List<String> names, String orgCode, Long tenentId);

    @Query(value = "select distinct biz_name from wwyt_workflow_process where delete_flag = '0'and location_service_open = 1 and org_code like concat(:orgCode,'%') and tenent_id = :tenentId",nativeQuery = true)
    List<String> findBizNamesByLocationServiceOpen(String orgCode, Long tenentId);

    List<WorkFlowProcess> findByBizTypeName(String bizTypeName);

    @Query(value = "select * from wwyt_workflow_process where delete_flag = '0'and location_service_open = 1 and id in :pids", nativeQuery = true)
    List<WorkFlowProcess> findByIdsAndLocationServiceOpen(@Param("pids") List<Long> pids);

    @Query(value = "select  id from wwyt_workflow_process where delete_flag = '0'and  location_service_open = 1 and biz_type_name = ?1", nativeQuery = true)
    List<Long> fidnIdsByBizTypeNameAndLocationServiceOpen(String bizName);

    @Query(value = "select id as `value`, biz_name as label from wwyt_workflow_process where delete_flag = '0'", nativeQuery = true)
    List<Tuple> findAllProcessIdAndName();

    @Query(value = "select biz_name from wwyt_workflow_process where delete_flag = '0' and id in :processIds", nativeQuery = true)
    List<String> findNameByIds(List<Long> processIds);

    List<WorkFlowProcess> findByCodeStrategyId(Long id);

    @Query(value = "SELECT p.* FROM wwyt_workflow_process p INNER JOIN wwyt_workflow_screen_config c ON p.id = c.process_id WHERE p.biz_type_name = ?1 AND c.type_id = ?2 AND p.delete_flag = '0'AND p.location_service_open=1 AND c.delete_flag = '0'", nativeQuery = true)
    List<WorkFlowProcess> findByBizTypeNameAndTypeId(String bizTypeName, Long typeId);
    @Query(value = "select biz_name from wwyt_workflow_process where delete_flag = '0' and definition_key = ?1", nativeQuery = true)
    WorkFlowProcess getWorkFlowProcessByDefinitionKey(String definitionKey);
    @Query(value = "select * from wwyt_workflow_process where delete_flag = '0' and biz_name = ?1", nativeQuery = true)
    List<WorkFlowProcess> findByBizNameList(String bizName);
}
